package com.zte.cl.task;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.zte.cl.service.NewsService;
import com.zte.cl.service.PushService;
import com.zte.cl.utils.ConstantUtil;

@Service
public class PushTask {

	private static final Log log = LogFactory.getLog(PushTask.class);
	
	@Resource
	private NewsService newsService;
	
	@Resource
	private PushService pushService;
	
	/**
	 * 推送新闻，每天早上8点和下午4点
	 */
	@Scheduled(cron = "0 0 8,16 * * ?")
	public void pushNews() {
		try {
			log.info("开始执行新闻推送定时任务...");
			// 重新加载配置文件
			ConstantUtil.reload();
			
			Map<String, String> pushMsg = null;
			String[] keywords = ConstantUtil.NEWS_KEYWORD.split(",");
			log.info("keywords = " + ConstantUtil.NEWS_KEYWORD);
			pushMsg = newsService.buildPushNews(keywords);
			if(pushMsg != null) {
				pushService.pushMsg(pushMsg);
			}
		} catch (JSONException e) {
			log.error("json数据错误", e);
		} catch (Exception e) {
			log.error("推送新闻时发生错误", e);
		}
	}
}
